// 二分查找 时间复杂度 O(logN)
const binarySearch = (arr, target) => {
  if (arr.length === 0) {
    return -1;
  }
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let mid = left + Math.floor((right - left) / 2);
    if (target > arr[mid]) {
      left = mid + 1;
    } else if (target < arr[mid]) {
      right = mid - 1;
    } else {
      return mid;
    }
  }
  return -1;
};

const test = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(binarySearch(test, 6));
